package com.sdehua.wear.service.impl;

import com.sdehua.wear.dao.GoodsDao;
import com.sdehua.wear.dao.TicketDao;
import com.sdehua.wear.dao.UserDao;
import com.sdehua.wear.entity.*;
import com.sdehua.wear.dao.MyorderDao;
import com.sdehua.wear.service.MyorderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Myorder)表服务实现类
 *
 * @author makejava
 * @since 2021-12-31 15:29:41
 */
@Service("myorderService")
public class MyorderServiceImpl implements MyorderService {
    @Resource
    private MyorderDao myorderDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private UserDao userDao;
    @Resource
    private TicketDao ticketDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Myorder queryById(Integer id) {
        return this.myorderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Myorder> queryAllByLimit(int offset, int limit) {
        return this.myorderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param myorder 实例对象
     * @return 实例对象
     */
    @Override
    public Myorder insert(Myorder myorder) {
        this.myorderDao.insert(myorder);
        return myorder;
    }

    /**
     * 修改数据
     *
     * @param myorder 实例对象
     * @return 实例对象
     */
    @Override
    public Myorder update(Myorder myorder) {
        this.myorderDao.update(myorder);
        return this.queryById(myorder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.myorderDao.deleteById(id) > 0;
    }

    /**
     * @MethodName addOrder
     * @Author coffee
     * @Description 添加订单
     * @Date 16:21 2022/1/1
     * @Param [userId, goodsId]
     * @return boolean
     **/
    @Override
    public boolean addOrder(String userId, String goodsId) {
        if (userId.isEmpty() || goodsId.isEmpty()) return false;
        Goods goods = goodsDao.queryById(Integer.parseInt(goodsId));
        Myorder myorder = new Myorder();
        myorder.setState(0);
        myorder.setTotalPrice(goods.getPrice());
        myorder.setUserId(Integer.parseInt(userId));
        myorder.setGoodsId(Integer.parseInt(goodsId));
        myorder.setCount(1);
        int insert = myorderDao.insert(myorder);
        if (insert<1) return false;
        return true;
    }

    @Override
    public List<CartVO> queryAllByUserId(String userId) {
        if (userId.isEmpty()) return null;
        Myorder myorder = new Myorder();
        myorder.setUserId(Integer.parseInt(userId));
        List<CartVO> cartVOs = myorderDao.queryAllByUserId(userId);
        if (cartVOs.size()<1) return null;
        return cartVOs;
    }

    @Override
    public boolean pay(int id,double money,Integer count) {
        Myorder myorder = myorderDao.queryById(id);
        myorder.setState(1);// 支付状态
        myorder.setCount(count);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        myorder.setTime(simpleDateFormat.format(date.getTime()));
        int update = myorderDao.update(myorder);
        if (update<1) return false;
        return true;
    }

    @Override
    public boolean payMe(int id,double money,Integer count,String ticketId) {
        Myorder myorder = myorderDao.queryById(id);
        myorder.setState(1);
        myorder.setCount(count);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/mm/dd HH:MM");
        myorder.setTime(simpleDateFormat.format(date));
        User user = userDao.queryById(myorder.getUserId());
        if (user.getMoney() < money) return false;
        user.setMoney(user.getMoney()-money);
        user.setScore(user.getScore()+(int)(money/100));
        if (!ticketId.equals("-1")){
            int i = ticketDao.deleteById(Integer.parseInt(ticketId));
            if (i < 1) return false;
        }
        user.setTicket(user.getTicket()-1);
        int update = myorderDao.update(myorder);
        if (update<1) return false;
        int update1 = userDao.update(user);
        if (update1<1) return false;
        return true;
    }

    @Override
    public List<CartVO> queryAllByUserIdAndState(String userId, String state) {
        if (userId.isEmpty() || state.isEmpty()) return null;
        Integer id = Integer.parseInt(userId);
        Integer stateId = Integer.parseInt(state);
        List<CartVO> cartVOs = myorderDao.queryAllByUserIdAndState(id,stateId);
        if (cartVOs.size()<1) return null;
        return cartVOs;
    }

    @Override
    public boolean getIt(String id) {
        Myorder myorder = new Myorder();
        myorder.setId(Integer.parseInt(id));
        myorder.setState(2);
        int update = myorderDao.update(myorder);
        System.out.println(myorder);
        if (update<1) return false;
        return true;
    }
}